package clientService;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI extends JFrame {

	private JPanel contentPane;
	private JTextField txtUser;
	private JTextField txtLocalhost;
	private JTextField textField;
	private JButton btnConnect;
	private JLabel lblLabel;
	private JLabel lblConnection;
	private String result;
	private JButton btnOrders;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI frame = new GUI();
					frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
					frame.setVisible(true);
					frame.btnOrders.setVisible(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 736, 463);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);

		txtUser = new JTextField();
		txtUser.setText("User");
		GridBagConstraints gbc_txtUser = new GridBagConstraints();
		gbc_txtUser.insets = new Insets(0, 0, 5, 5);
		gbc_txtUser.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtUser.gridx = 7;
		gbc_txtUser.gridy = 1;
		contentPane.add(txtUser, gbc_txtUser);
		txtUser.setColumns(10);

		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new Thread(new Runnable() {
					public void run() {
						result = ClientApp.login(txtUser.getText());
						System.out.println("result " + result);
						SwingUtilities.invokeLater(new Runnable() {

							public void run() {
								btnOrders.setVisible(true);
								lblLabel.setText(result);
							}
						});
					}
				}).start();
			}
		});
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.insets = new Insets(0, 0, 5, 0);
		gbc_btnLogin.gridx = 9;
		gbc_btnLogin.gridy = 1;
		contentPane.add(btnLogin, gbc_btnLogin);

		lblLabel = new JLabel("label");
		GridBagConstraints gbc_lblLabel = new GridBagConstraints();
		gbc_lblLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblLabel.gridx = 7;
		gbc_lblLabel.gridy = 2;
		contentPane.add(lblLabel, gbc_lblLabel);
		
				btnConnect = new JButton("Connect");
				btnConnect.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						new Thread(new Runnable() {
							public void run() {
								result = ClientApp.init(txtLocalhost.getText(),
										Integer.parseInt(textField.getText()));
								System.out.println("result " + result);
								SwingUtilities.invokeLater(new Runnable() {

									public void run() {
										lblConnection.setText(result);
									}
								});
							}
						}).start();
					}
				});
				
						txtLocalhost = new JTextField();
						txtLocalhost.setText("localhost");
						GridBagConstraints gbc_txtLocalhost = new GridBagConstraints();
						gbc_txtLocalhost.insets = new Insets(0, 0, 5, 5);
						gbc_txtLocalhost.fill = GridBagConstraints.HORIZONTAL;
						gbc_txtLocalhost.gridx = 7;
						gbc_txtLocalhost.gridy = 4;
						contentPane.add(txtLocalhost, gbc_txtLocalhost);
						txtLocalhost.setColumns(10);
				GridBagConstraints gbc_btnConnect = new GridBagConstraints();
				gbc_btnConnect.insets = new Insets(0, 0, 5, 0);
				gbc_btnConnect.gridx = 9;
				gbc_btnConnect.gridy = 4;
				contentPane.add(btnConnect, gbc_btnConnect);
		
				textField = new JTextField();
				textField.setText("5090");
				GridBagConstraints gbc_textField = new GridBagConstraints();
				gbc_textField.insets = new Insets(0, 0, 5, 5);
				gbc_textField.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField.gridx = 7;
				gbc_textField.gridy = 5;
				contentPane.add(textField, gbc_textField);
				textField.setColumns(10);
		
				lblConnection = new JLabel("Connection");
				GridBagConstraints gbc_lblConnection = new GridBagConstraints();
				gbc_lblConnection.insets = new Insets(0, 0, 5, 5);
				gbc_lblConnection.gridx = 7;
				gbc_lblConnection.gridy = 6;
				contentPane.add(lblConnection, gbc_lblConnection);
				
				btnOrders = new JButton("Orders");
				btnOrders.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						new Thread(new Runnable() {
							public void run() {
								OrdersFrame.main();
							}
						}).start();
					}
				});
				GridBagConstraints gbc_btnOrders = new GridBagConstraints();
				gbc_btnOrders.insets = new Insets(0, 0, 5, 0);
				gbc_btnOrders.gridx = 9;
				gbc_btnOrders.gridy = 8;
				contentPane.add(btnOrders, gbc_btnOrders);
	}

}
